package public

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/public"
	publicReq "gitcode.com/G-YT/ANN/server/model/public/request"
	"gitcode.com/G-YT/ANN/server/model/user"
	"gitcode.com/G-YT/ANN/server/utils"
)

type MessageService struct {
}

// 方法使用示例
/*


var messageService = service.ServiceGroupApp.PublicServiceGroup.MessageService

message:=public.Message{}
message.To="all或者手机号"
message.Type="system"

tb, err := json.Marshal(map[string]interface{}{
		"like":    "点赞",
		"comment": "评论",
	})

message.Info = tb
messageService.CreateMessage(&message)
*/

// CreateMessage 创建消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) CreateMessage(message *public.Message) (err error) {
	messages := []public.UserMessage{}
	users := []user.User{}
	if message.To == "all" {
		err = global.GVA_DB.Find(&users).Error
		if err != nil {
			return err
		}
		for _, cu := range users {
			var userMessage public.UserMessage
			userMessage.Type = message.Type
			userMessage.Info = message.Info
			userMessage.To = cu.Phone
			userMessage.UserID = cu.ID
			userMessage.Readed = utils.Pointer(false)
			messages = append(messages, userMessage)
		}
		err = global.GVA_DB.Create(messages).Error
		if err != nil {
			return err
		}
	} else {
		var du user.User
		var userMessage public.UserMessage
		err = global.GVA_DB.Where("phone = ?", message.To).First(&du).Error
		if err != nil {
			return err
		}
		userMessage.Type = message.Type
		userMessage.Info = message.Info
		userMessage.To = du.Phone
		userMessage.UserID = du.ID
		userMessage.Readed = utils.Pointer(false)
		err = global.GVA_DB.Create(&userMessage).Error
		if err != nil {
			return err
		}
	}
	err = global.GVA_DB.Create(message).Error
	return err
}

// DeleteMessage 删除消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) DeleteMessage(message public.Message) (err error) {
	err = global.GVA_DB.Delete(&message).Error
	return err
}

// DeleteMessageByIds 批量删除消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) DeleteMessageByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]public.Message{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateMessage 更新消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) UpdateMessage(message public.Message) (err error) {
	err = global.GVA_DB.Save(&message).Error
	return err
}

// GetMessage 根据id获取消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) GetMessage(id uint) (message public.Message, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&message).Error
	return
}

func (messageService *MessageService) GetUserMessage(id uint, userID uint) (message public.UserMessage, err error) {
	err = global.GVA_DB.Where("id = ? and user_id = ?", id, userID).First(&message).Update("readed", utils.Pointer(true)).Error
	return
}

func (messageService *MessageService) GetUserMessageInfoList(info publicReq.MessageSearch, userID uint, userAuth uint) (list []public.UserMessage, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&public.UserMessage{})
	var messages []public.UserMessage
	// 如果有条件搜索 下方会自动创建搜索语句
	if userAuth != 888 {
		db = db.Where("user_id = ?", userID)
	}
	if info.Type != "" {
		db = db.Where("type = ?", info.Type)
	}
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&messages).Error
	return messages, total, err
}

// GetMessageInfoList 分页获取消息记录
// Author [piexlmax](https://github.com/piexlmax)
func (messageService *MessageService) GetMessageInfoList(info publicReq.MessageSearch) (list []public.Message, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&public.Message{})
	var messages []public.Message
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&messages).Error
	return messages, total, err
}

// GetUnReadMessageNumber 获取未读消息数
func (messageService *MessageService) GetUnReadMessageNumber(userId uint) (total int64, err error) {
	err = global.GVA_DB.Model(&public.UserMessage{}).Where("readed = ? and user_id = ?", 0, userId).Count(&total).Error
	return
}
